%function Full_Welfare

clear
load resultsFMIN.mat

clearvars snPass

%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;
% 1. Getting ready for graphs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;

% Cost parameters (in thousand Kwacha);
cp.alpha  = 48;           % Public benefit per tree;
cp.eta    = 1.2;          % Cost of public funds;
cp.mon    = 35;           % Monitoring cost;
cp.cb     = (1/(1+0.03)); % Cost of borrowing;

% Run parameters, tolerance is unchanged
rp.parl    = 0;
rp.m       = 200;
rp.k       = 1;
rp.lam     = 0;
rp.theseed = rp.theseed+1000;


rng(rp.theseed);
snPass.s       = rand(obs*(3*rp.k + rp.m),1);

% These two lines create a (k x m x 51) matrix, where the first floor is a
% (k x m) matrix of ones, the second a (k x m) matrix of twos, up to 51;
% Nv is just N repeated k x 51 times;
% the fourth and fifth are true false matrices for N vs. Nbar;
Ntr            = permute(N,[3,1,2]);
snPass.N3D     = Ntr(ones(rp.k,1),ones(rp.m,1),:);
snPass.Nv      = N(ones(rp.k,1),:);
snPass.Ngeq3D  = (snPass.N3D>=Nbar);
snPass.N03D    = (snPass.N3D>0);
snPass.Ngeqv   = (snPass.Nv>=Nbar);
snPass.N0v     = (snPass.Nv>0);

% Add cluster (group/village) data
%load('Matlab_2014_12_cluster.mat')
%group = st_dataset(:,58);
%group = double(group);




%%
% Must reset several values, setting to 0: F_surp (F_treat), F_moni, and
% F_shift -- This removes this context specific shifters. Further, must
% redefine Nmatrices because of new k/m dimensions

theta_hat0 = [theta_hat2(1:6),0,0,0]; % Ensures F_* parameters = 0

TG0   = zeros(obs,1);      % Alternate treatment vector, set = 0
MON0  = zeros(obs,1);      % Alternate monitoring vector, set = 0


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2a) Varying Uncertainty on expected profits and trees
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Avalues_G1  = [0,12,0,12];
Rvalues_G1  = [0,0,150,150];
if length(Avalues_G1)==length(Rvalues_G1)
    sAR      = length(Rvalues_G1);
end

sd_F1_G1 = 0:20:500;
ssdf1    = length(sd_F1_G1);
counter  = 1;

for j=1:sAR
    for l=1:ssdf1
        sd_F1unc = sd_F1_G1(l);
        
        Aunc     = Avalues_G1(j).*ones(obs,1);
        Runc     = Rvalues_G1(j).*ones(obs,1);
        
        theta_hat_G1 = coefftran(theta_hat0,0);
        theta_hat_G1(5) = sd_F1unc;
        theta_run    = theta_hat_G1;
        
        [PartProb,Npdf,Nobs,Prof1,Prof0,EN,OV,Prof0_C] = Full_Simulate(theta_run,DP,Aunc,Runc,TG0,MON0,snPass,rp);
        
        [mW_u(l,j),mP_u(l,j),mProf0_u(l,j),mPProf0_u(l,j),mProf0_C_u(l,j),mPProf1_u(l,j),mPN_u(l,j),mPComp_u(l,j),mPOV_u(l,j)] =...
            zambia_postanalyze(cp,rp,Aunc,Runc,Npdf,PartProb,Nobs,Prof1,Prof0,EN,OV,Prof0_C);
        
        obsDR   = ~PartProb;
        NobsDR  = Nobs;
        NobsDR(obsDR)=[];

        m00C_u(l,j) = mean((NobsDR==0))/mP_u(l,j);     
        m50C_u(l,j) = mean((NobsDR==50))/mP_u(l,j);
                 
        counter=counter+1
    end
end
%%
save 'postest_welfare_figs.mat'
%%
load postest_welfare_figs.mat
%%
tranvec = coefftran(theta_hat2,0);
fig3 = zambia_figNumTr(sd_F1_G1,mP_u,mPComp_u,mPN_u,mW_u,mPProf0_u,mProf0_C_u,tranvec(5));
export_fig('fig3_new.png','-m2','-painters',fig3)
%export_fig 'fig3.png' '-m2' '-painters' '-transparent'

%%
close all
fig4 = zambia_figPartComp(sd_F1_G1,mP_u,mPComp_u,mPN_u,mW_u,mPProf0_u,mProf0_C_u,tranvec(5));
export_fig('fig4_new.png','-m2','-painters',fig4)
%export_fig 'fig4.png' '-m2' '-painters' '-transparent'
%export_fig 'fig4.png' '-m2' '-transparent'

 
%%
close all
fig5 = zambia_figExpPr(sd_F1_G1,mPProf0_u,mPOV_u,tranvec(5));
%export_fig('fig5.png','-m2','-painters',fig5)
%export_fig 'fig5.png' '-m2' '-painters' '-transparent'

%% ALTERATE FIGURE


load postest_welfare_figs.mat
tranvec = coefftran(theta_hat2,0);
%%
%close all
%fig6 = zambia_figPartComp2(sd_F1_G1,mP_u,mPComp_u,mPN_u,mW_u,mPProf0_u,mProf0_C_u,tranvec(5));
export_fig('fig6_new.png','-m2','-painters',fig6)

%%
fig7 = zambia_figPartComp3(sd_F1_G1,mP_u,mPComp_u,mPN_u,mW_u,mPProf0_u,mProf0_C_u,tranvec(5));
export_fig('fig7_new.png','-m2','-painters',fig7)

%%

fig8 = zambia_figExpPr2(sd_F1_G1,mPProf0_u,mPOV_u,tranvec(5));
export_fig('fig8.png','-m2','-painters',fig8)




